// copyright
package com.cicadamob.contact.api.service;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.Context;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;

/**
 * 类的描述:将联系人和联系电话，存放到本地
 * @author y m 
 * @version 创建时间：2013-12-31 下午5:52:39 
 */
public class SaveToLocalUtils {
	public static boolean saveToLocal(Context ctx,String name,String phone){
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = ctx.getContentResolver();
		ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
		ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
				.withValue("account_name", null).build();
		operations.add(op1);

		uri = Uri.parse("content://com.android.contacts/data");
		ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
				.withValueBackReference("raw_contact_id", 0)
				.withValue("mimetype", "vnd.android.cursor.item/name")
				.withValue("data2", name).build();
		operations.add(op2);

		ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
				.withValueBackReference("raw_contact_id", 0)
				.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
				.withValue("data1", phone).withValue("data2", "2")
				.build();
		operations.add(op3);
		
		try {
			resolver.applyBatch("com.android.contacts", operations);
		} catch (RemoteException e) {
			Log.i("tag", "到这里RemoteException");
			e.printStackTrace();
			return false;
		} catch (OperationApplicationException e) {
			Log.i("tag", "到这里OperationApplicationException");
			e.printStackTrace();
			return false;
		}
		System.out.println("保存完毕");
		return true;
	}
}
